<html>
<head><meta charset="utf-8"><title>MVP Generators use-case · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html">MVP Generators use-case</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="237532966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237532966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237532966">(May 05 2021 at 17:18)</a>:</h4>
<p>Hey <span class="user-mention" data-user-id="119031">@Esteban Küber</span> ! I know we're planning to talk about generators at a lang design team meeting soon. As I understand it, the primary motivation here is for use with <code>async gen fn</code> or <code>async gen</code> blocks. I'm curious to understand the specific use-case we're trying to unblock better. If you have a chance, I'd love to chat with you here (on zulip) or pre-read anything you've had a chance to write so far! I have a feeling that I will have weeds-y followup questions that wouldn't necessarily be a good use of folks time during the synchronous lang design meeting, and would prefer to cover them ahead of time if possible. Thanks in advance for any additional info you're able to provide :)</p>
<p>cc <span class="user-mention" data-user-id="116009">@nikomatsakis</span></p>



<a name="237538879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237538879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237538879">(May 05 2021 at 17:58)</a>:</h4>
<p>I think there are a few key questions -- do we want to target iterators specifically or more than iterators?</p>



<a name="237538903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237538903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237538903">(May 05 2021 at 17:58)</a>:</h4>
<p>(streams being "async interators")</p>



<a name="237539002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237539002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237539002">(May 05 2021 at 17:59)</a>:</h4>
<p>I think that is a good focus.</p>



<a name="237539038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237539038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237539038">(May 05 2021 at 17:59)</a>:</h4>
<p>I'd like to have a few good examples to drive the conversation with though</p>



<a name="237551884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237551884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237551884">(May 05 2021 at 19:25)</a>:</h4>
<p>I think even "iterators or more than iterators" is perhaps a step too close for my question. I'd really like a more holistic understanding of the kinds of problems we'd like people to be able to solve, and whether language-native generators are the right tool to introduce to solve those problems.</p>



<a name="237599451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/237599451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#237599451">(May 06 2021 at 03:21)</a>:</h4>
<p>As a motivating example then.. avoiding having to write <a href="https://rust-lang.github.io/wg-async-foundations/vision/status_quo/alan_hates_writing_a_stream.html">manual stream impls</a> with state machines</p>



<a name="238336172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238336172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238336172">(May 11 2021 at 16:15)</a>:</h4>
<p><span class="user-mention" data-user-id="119031">@Esteban Küber</span> did you have a write-up prepared?</p>



<a name="238387669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238387669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238387669">(May 11 2021 at 21:54)</a>:</h4>
<p><span class="user-mention" data-user-id="119031">@Esteban Küber</span> do you mind if I post the hackmd you just shared with me so that we can discuss it here? (cc <span class="user-mention" data-user-id="116009">@nikomatsakis</span> )</p>



<a name="238387761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238387761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238387761">(May 11 2021 at 21:55)</a>:</h4>
<p>Of course, <br>
<a href="https://hackmd.io/9v81TQSgQcaAiqvHQtzN8w?both">https://hackmd.io/9v81TQSgQcaAiqvHQtzN8w?both</a></p>



<a name="238388092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238388092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238388092">(May 11 2021 at 21:58)</a>:</h4>
<p>Okay-- after reading this i still think I don't understand the urgency behind this feature. It's already possible to write Streams (even more easily than iterators) using this syntax today, provided that you're (a) comfortable with the cost of a single allocation and (b) comfortable with having the resulting Stream being a voldemort type.</p>



<a name="238388335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238388335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238388335">(May 11 2021 at 22:00)</a>:</h4>
<p>I think (b) is orthogonal to this proposal. The solution to that is type-alias <code>impl Trait</code>, which is a separate feature.</p>



<a name="238388711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238388711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238388711">(May 11 2021 at 22:03)</a>:</h4>
<p>I think that a number of the library examples provided would likely still continue writing a manual stream given that they're incredibly common and size optimization and nameability are critical. I think that we're primarily worried about improving end-user (binary author or high-level library author) ergonomics, not the ergonomics of folks writing things like <code>futures-util</code>.</p>



<a name="238388986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238388986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238388986">(May 11 2021 at 22:06)</a>:</h4>
<p>In practice, I think that audience (high-level libraries and binary authors) is mostly unconcerned with a single allocation per stream</p>



<a name="238389120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389120">(May 11 2021 at 22:07)</a>:</h4>
<p>Wait, I feel like I'm lost on which parts you're commenting on</p>



<a name="238389244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389244">(May 11 2021 at 22:08)</a>:</h4>
<p>I'm primarily responding to the "Why?" section:</p>
<div class="codehilite"><pre><span></span><code>The main impetus for this is that these kind of algorithms are common enough in both library and end user code, writing it by hand can be verbose to express, writing a `Stream` requires interaction with `Pin`, and transforming an `Iterator` implementation into a `Stream` is non-trivial. The desire is to reduce the pain of implementing new async iterators, but if the feature is introduced it makes sense to also support sync iterators.
</code></pre></div>



<a name="238389381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389381">(May 11 2021 at 22:10)</a>:</h4>
<p>Re: end-user vs lib writer: I agree that lib writers would 1) delay using this even if available to support older rustc versions and 2) have a higher tolerance for "pain"</p>



<a name="238389456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389456">(May 11 2021 at 22:10)</a>:</h4>
<p>but I posit that depending on what you call an "end-user", they are already needing them</p>



<a name="238389510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389510">(May 11 2021 at 22:10)</a>:</h4>
<p>for example, miniredis uses async_stream to write combinators</p>



<a name="238389539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389539">(May 11 2021 at 22:11)</a>:</h4>
<p>there are verticals where dealing with streams <em>is</em> the main function of the application</p>



<a name="238389570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389570">(May 11 2021 at 22:11)</a>:</h4>
<blockquote>
<p>miniredis uses async_stream to write combinators</p>
</blockquote>
<p>Is that a problem?</p>



<a name="238389690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389690">(May 11 2021 at 22:12)</a>:</h4>
<p>not a problem, per se, a signal that there's a need being currently covered by a few different crates that in my eyes is a core concern of some users</p>



<a name="238389725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389725">(May 11 2021 at 22:13)</a>:</h4>
<p>Are the users concerned that they have to reach for a crate in order to get that functionality, or are we concerned that the functionality isn't discoverable enough?</p>



<a name="238389750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389750">(May 11 2021 at 22:13)</a>:</h4>
<p>Or something else?</p>



<a name="238389837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389837">(May 11 2021 at 22:14)</a>:</h4>
<p>just "use a crate" <em>is</em> a valid position, but there are implications not only on the learnability and UX (thinking of diagnostics, as usual), but also on future direction for something like <code>for await x in foo { //&lt;- foo gets pin_mut!ed</code></p>



<a name="238389896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389896">(May 11 2021 at 22:15)</a>:</h4>
<p>users are concerned about crates, that's definitely a common theme on every medium we've used to collect peoples thoughts</p>



<a name="238389914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389914">(May 11 2021 at 22:15)</a>:</h4>
<p>(FWIW I actively do not want <code>for await x in foo</code>, since I think it is usually a mistake to process stream elements serially)</p>



<a name="238389981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238389981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238389981">(May 11 2021 at 22:15)</a>:</h4>
<p>but discoverability is as big an issue, and how the desugared code of these crates interacts with invalid code can be honestly inescrutable</p>



<a name="238390037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390037">(May 11 2021 at 22:16)</a>:</h4>
<p>FWIW you don't need a macro to provide this functionality at all</p>



<a name="238390062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390062">(May 11 2021 at 22:16)</a>:</h4>
<p>You can do it by passing an argument into the future that you <code>.await</code> to yield a result</p>



<a name="238390158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vincent Isambart <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390158">(May 11 2021 at 22:17)</a>:</h4>
<p>In the draft RFC you are saying you "reject the implementation of generalized coroutines" but I was wondering where do you draw the distinction between generators and generalized coroutines. When going back to the coroutine being able to pass a value back to it or not?</p>



<a name="238390246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390246">(May 11 2021 at 22:18)</a>:</h4>
<p>being able to return values through a yield point and non-<code>()</code> return types</p>



<a name="238390262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390262">(May 11 2021 at 22:18)</a>:</h4>
<p>both of which have implications on the API surface for the generator itself</p>



<a name="238390305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390305">(May 11 2021 at 22:19)</a>:</h4>
<p>but if providing <em>only</em> something that people can iterate over to build combinators, those two things aren't necessary</p>



<a name="238390324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390324">(May 11 2021 at 22:19)</a>:</h4>
<p>but if the final syntax is thoughtful, then it could be added in the future</p>



<a name="238390442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390442">(May 11 2021 at 22:20)</a>:</h4>
<p>but passing back a value through a yield also raises questions about how you would interact with the generators: you wouldn't be able to do <code>for</code> or <code>while let</code> on them, not directly</p>



<a name="238390530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390530">(May 11 2021 at 22:21)</a>:</h4>
<p>if you look at <a href="https://hackmd.io/9v81TQSgQcaAiqvHQtzN8w?both#Status-quo-featuregenerators">https://hackmd.io/9v81TQSgQcaAiqvHQtzN8w?both#Status-quo-featuregenerators</a> you can see what the api is today that would need to be extended slightly to potentially let you interact with it</p>



<a name="238390653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238390653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vincent Isambart <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238390653">(May 11 2021 at 22:23)</a>:</h4>
<p>Makes sense, thanks.</p>



<a name="238391073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238391073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238391073">(May 11 2021 at 22:27)</a>:</h4>
<p>Another consideration that I fail to cover thoroughly is an appeal to "error proness": <a href="https://nikomatsakis.github.io/wg-async-foundations/vision/status_quo/aws_engineer/missed_waker_leads_to_lost_performance.html">people make mistakes when implementing Stream by hand</a> and even when possible, <a href="https://nikomatsakis.github.io/wg-async-foundations/vision/status_quo/alan_hates_writing_a_stream.html">it is not a nice overall experience</a></p>



<a name="238392076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392076">(May 11 2021 at 22:37)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=879d9f4f8e6e86f978fb3115db1531a9">A playground example of one way you can avoid manually writing streams by hand, today, without macros</a></p>



<a name="238392196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392196">(May 11 2021 at 22:38)</a>:</h4>
<p>The stream created there can reference non-static data, and the only allocation required is for the channel. You could probably optimize that one out as well if you were willing to be a bit more crafty about constructing the <code>fut</code> element in-place with a pointer to the other field.</p>



<a name="238392258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392258">(May 11 2021 at 22:39)</a>:</h4>
<p>The only library being used there is <code>futures</code>, and the only "interesting" thing being pulled in is the channel implementation</p>



<a name="238392338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392338">(May 11 2021 at 22:40)</a>:</h4>
<p>( I used <code>pin_project</code> to make it easier, too, but you could get rid of that in exchange for two lines of unsafe code)</p>



<a name="238392535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392535">(May 11 2021 at 22:42)</a>:</h4>
<p>In practice I'd want a version that required a future which returned <code>Result&lt;(), E&gt;</code> as well so that users could use <code>?</code> in the implementation, but that's a pretty simple modification.</p>



<a name="238392764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238392764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238392764">(May 11 2021 at 22:44)</a>:</h4>
<p>All this isn't to say that I don't want Rust to provide some kind of syntax for natively expressing these kinds of things, but that I think it is a relatively low priority despite being a huge and controversial design space.</p>



<a name="238537793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238537793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238537793">(May 12 2021 at 20:19)</a>:</h4>
<p>I've only skimmed the document, but I have a passing question. I've been writing a parser recently that effectively is an iterator backed by a giant state machine (notably it's <strong>not</strong> <code>Iterator</code> because of the lack of GATs). I'm writing that state machine <em>by hand</em>, which is tedious. I haven't yet played with trying to write it in today's version of generators, but the analogies feel apparent to me.</p>
<p>I know that was handwavy, but does it seem like the proposed MVP generators would be able to come to bear in my situation?</p>



<a name="238539199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238539199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238539199">(May 12 2021 at 20:28)</a>:</h4>
<p>No, the proposed MVP does not support yielding references into the generator state</p>



<a name="238539207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238539207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238539207">(May 12 2021 at 20:28)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span></p>



<a name="238539209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238539209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238539209">(May 12 2021 at 20:28)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> the proposal tries to leave the door open on being able to pass data back into the generator and having a separate return value, but tries to stay away from it</p>



<a name="238539236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238539236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238539236">(May 12 2021 at 20:28)</a>:</h4>
<p>it would let you implement a lexer, but not a parser</p>



<a name="238539454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238539454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238539454">(May 12 2021 at 20:30)</a>:</h4>
<p>Shame. Thank you!</p>



<a name="238541036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238541036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238541036">(May 12 2021 at 20:41)</a>:</h4>
<p>Seeing the chatter about the ability to add methods (e.g. <code>size_hint</code>) / have a named implementation, that's something that I think would be <em>huge</em>. That's a reason I offer for people to avoid using <code>impl trait</code> in public APIs.</p>



<a name="238546769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238546769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238546769">(May 12 2021 at 21:22)</a>:</h4>
<p>(It also reminded me a conversation we had once -- I forget when -- about an explicit capture syntax for closures potentially being able to expose those captures as fields on the voldemort type.  Maybe annotating <code>let</code>s could do that for generators, too?  Scares me about the borrow-checking safety implications, though...)</p>



<a name="238547337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238547337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238547337">(May 12 2021 at 21:28)</a>:</h4>
<p>Something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">closure</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">captured</span><span class="p">.</span><span class="n">field</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">closure</span><span class="p">.</span><span class="n">foo</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>?</p>



<a name="238547743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238547743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238547743">(May 12 2021 at 21:31)</a>:</h4>
<p>Well, at the time we were talking about <code>let closure = move[&amp;mut x] || { *x += 1 }; closure.x</code>, but yeah, <code>#[magic_☃_annotation] let foo</code> (perhaps <code>pub</code>) would make more sense for generators and would work for closures too.</p>



<a name="238564999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238564999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238564999">(May 13 2021 at 00:38)</a>:</h4>
<p>This reminds me that I want to resurrect an explicit capture syntax</p>



<a name="238565036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565036">(May 13 2021 at 00:39)</a>:</h4>
<p>i liked the idea of <code>pub let</code>, but then I realized that it...kind of makes no sense, since the closure may not have excuted.</p>



<a name="238565063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565063">(May 13 2021 at 00:39)</a>:</h4>
<p>Another idea that has been kicked around in this space from Ye Olde Days was having <code>impl Trait { .. }</code> expressions, analogous to java nested objects</p>



<a name="238565118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565118">(May 13 2021 at 00:40)</a>:</h4>
<p>However, it does not directly address the problem of making generators easier to use</p>



<a name="238565141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565141">(May 13 2021 at 00:40)</a>:</h4>
<p>BTW, I was talking to <span class="user-mention" data-user-id="119031">@Esteban Küber</span> and I suggested that he could try to extract and summarize the major points from the meeting into a blog post</p>



<a name="238565154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565154">(May 13 2021 at 00:40)</a>:</h4>
<p>I think it's generally a nice practice, but one that would be particularly useful in this case</p>



<a name="238565893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238565893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238565893">(May 13 2021 at 00:50)</a>:</h4>
<p>I'll have it tomorrow, and will run it by the t-lang team. What should be the "intended audience"? How much context should I add at the beginning?</p>



<a name="238566521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/238566521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#238566521">(May 13 2021 at 01:00)</a>:</h4>
<p>I'd expect it'd go on the inside rust blog and would be targeted at roughly "people interested in this area" - so not too much context, mostly more like a digested form of meeting minutes. But maybe Niko or other's have different opinions here.</p>



<a name="239179919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/239179919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#239179919">(May 18 2021 at 00:29)</a>:</h4>
<p>That is what I had in mind too</p>



<a name="239551775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/MVP%20Generators%20use-case/near/239551775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/MVP.20Generators.20use-case.html#239551775">(May 20 2021 at 08:12)</a>:</h4>
<p>I'm not entirely sure if this is possible with the MVP, but I actually had a case that I think would be a lot simpler with generators that doesn't involve async.</p>
<p>I'm writing an codec framework for ASN.1 and one of the more annoying types is <code>SET</code>, which for some (not all) formats requires that the components are serialized in ascending order of their "tag". I was thinking that a nice way to prevent people from making this logic bug, would be to have it accept a generator that would yield the results of each field being encoded.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// This struct could be encoded as `b, a` or `a, b`.</span>
<span class="k">struct</span> <span class="nc">Set</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[rasn(tag(1))]</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[rasn(tag(0))]</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Set encode impl</span>
<span class="k">fn</span> <span class="nf">encode</span><span class="p">(</span><span class="n">value</span>: <span class="nc">Set</span><span class="p">,</span><span class="w"> </span><span class="n">enc</span>: <span class="nc">Encoder</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">enc</span><span class="p">.</span><span class="n">encode_set</span><span class="p">(</span><span class="n">gen</span><span class="o">*</span><span class="w"> </span><span class="o">|</span><span class="n">set_encoder</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kr">yield</span><span class="w"> </span><span class="n">set_encoder</span><span class="p">.</span><span class="n">encode_integer</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="kr">yield</span><span class="w"> </span><span class="n">set_encoder</span><span class="p">.</span><span class="n">encode_string</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">   </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// `b, a` encode_set impl</span>
<span class="k">fn</span> <span class="nf">encode_set</span><span class="p">(</span><span class="n">enc</span>: <span class="kp">&amp;</span><span class="nc">Encoder</span><span class="p">,</span><span class="w"> </span><span class="n">fields</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Stream</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">=</span><span class="p">(</span><span class="n">Tag</span><span class="p">,</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;&gt;</span><span class="p">)</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">map</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">BTreeMap</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="kr">yield</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">map</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">   </span><span class="nb">Ok</span><span class="p">(</span><span class="n">map</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">collect</span><span class="p">())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// `a, b` encode_set impl</span>
<span class="k">fn</span> <span class="nf">encode_set</span><span class="p">(</span><span class="n">enc</span>: <span class="kp">&amp;</span><span class="nc">Encoder</span><span class="p">,</span><span class="w"> </span><span class="n">fields</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Stream</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">=</span><span class="p">(</span><span class="n">Tag</span><span class="p">,</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;&gt;</span><span class="p">)</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">vec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="kr">yield</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">vec</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">field</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">   </span><span class="nb">Ok</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>